package controllers

import (
	"encoding/json"
	"gitchina/kly_service/models"
	"gitchina/kly_service/util"
	"github.com/astaxie/beego"
	"net/http"
)

type MockController struct {
	beego.Controller
}

// @router /order [post]
func (t *MockController) MockOrder() {
	var mock models.MockOrder
	err := json.Unmarshal(t.Ctx.Input.RequestBody, &mock)
	if err != nil {
		util.GetLogger().Error("mock error:%s", err.Error())
		http.Error(t.Ctx.ResponseWriter, err.Error(), http.StatusBadRequest)
	}
	err = mock.Mock()
	if err != nil {
		util.GetLogger().Error("mock error:%s", err.Error())
		t.Data["json"] = models.NewFailResponse(err)
	} else {
		t.Data["json"] = models.NewOkResponse(nil)
	}
	t.ServeJSON()
}

// @router /order/clean [post]
func (t *MockController) DeleteOrder() {
	var mock models.MockOrder
	err := json.Unmarshal(t.Ctx.Input.RequestBody, &mock)
	if err != nil {
		util.GetLogger().Error("mock error:%s", err.Error())
		http.Error(t.Ctx.ResponseWriter, err.Error(), http.StatusBadRequest)
	}
	mock.Delete()
	t.Data["json"] = models.NewOkResponse(nil)
	t.ServeJSON()
}
